<?php

// +----------------------------------------------------------------------
// | Apprh.Shop
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.apprh.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------
// | Author: 雲溪荏苒 <290648237@qq.com>
// +----------------------------------------------------------------------

namespace app\frontend\controller;

use controller\HomeBase;
use think\Validate;


/**
 * 留言反馈控制器
 *
 * Class Message
 * @package app\frontend\controller
 * @author 雲溪荏苒 <290648237@qq.com>
 * @date 2017/12/16
 */
class Message extends HomeBase
{

    /**
     * 留言反馈
     * @return mixed
     */
    public function message_add()
    {
        if(request()->isPost()) {
            $post = I('post.');

            // 验证规则
            $rules = [
                'name' => 'require',
                'mobile' => 'require',
                'email' => 'require|email',
                'content' => 'require'
            ];
            $msg = [
                'name.require' => '姓名不能为空',
                'mobile.require' => '电话不能为空',
                'email.require' => '邮箱不能为空',
                'email.email' => '邮箱格式错误',
                'content.require' => '留言内容不能为空',
            ];
            $validate = new Validate($rules, $msg);
            $validate->check($post);
            if (!empty($validate->getError())) {
                $this->error($validate->getError());
            }

            $post['create_at'] = time();
            $result = db('feedback')->insert($post);
            if (!$result) {
                $this->error('留言失败!');
            }
            $this->success('您已成功留言！');
        }
    }

}
